package org.dizena.modules.master.web;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.dizena.common.pojo.PageReq;
import org.dizena.common.pojo.PageResp;
import org.dizena.common.pojo.master.RoleVO;
import org.dizena.common.pojo.master.TreeVO;
import org.dizena.modules.master.bean.Role;
import org.dizena.modules.master.service.ResService;
import org.dizena.modules.master.service.RoleService;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 管理-角色
 */
@RestController
@RequiresPermissions("master:role:*")
@RequestMapping("/api/v1/role")
public class ApiRoleController
{
    @Resource
    private RoleService service;
    @Resource
    private ResService resService;


    /**
     * 角色-枚举
     */
    @PostMapping("list")
    public PageResp<RoleVO> list(PageReq req)
    {
        PageResp<Role> pageResp = service.list(req);
        List<Role> roles = pageResp.getList();
        List<RoleVO> list = new ArrayList<>();
        roles.forEach(role ->
        {
            List<TreeVO> treeVOList = resService.getRoleTreeData("0", 1, role.getResIds());
            RoleVO vo = new RoleVO();
            BeanUtils.copyProperties(role, vo);
            vo.setMenu(treeVOList);
            list.add(vo);
        });
        return new PageResp<>(pageResp.getPage(), pageResp.getSize(), pageResp.getCount(), list);
    }

    /**
     * 角色-新增
     */
    @PostMapping("add")
    public String add(@Validated Role m)
    {
        return service.add(m);
    }

    /**
     * 角色-删除
     */
    @PostMapping("del")
    public String del(String id)
    {
        return service.del(id);
    }

    /**
     * 角色-更新
     */
    @PostMapping("edit")
    public String edit(Role m)
    {
        return service.edit(m);
    }

    /**
     * 角色-明细
     */
    @PostMapping("get")
    public Role get(String id)
    {
        return service.get(id);
    }

    /**
     * 角色-列举
     */
    @PostMapping("all")
    public List<String> all()
    {
        return service.all();
    }

    /**
     * 权限-列举
     */
    @PostMapping("auth")
    public List<String> auth()
    {
        return service.auth();
    }
}
